package com.servertrack;
import java.util.Date;
import org.springframework.context.MessageSource

import com.commonProject.common.classes.TurDTO
import com.commonProject.common.exception.ValidateException
import com.commonProject.common.intefaces.ITur

class TurService implements ITur{
	static transactional = true
	MessageSource messageSource
	
	List<TurDTO> getlist(Long tur_id){
		//def el=Company.get(tur_id)		
	
		List<TurDTO> dtoList=new ArrayList<TurDTO>()
	
		TurDTO tur=null
		
		Tur.list().each{
			tur=new TurDTO()
			tur.title = it.title
			tur.id  = ("Tur_"+it.id)
			dtoList<<tur
		}
		return 	dtoList
	}	
	 
	
	TurDTO getelement(Long tur_id){
		def el=Tur.get(tur_id)
		
		TurDTO dto=new TurDTO()		
		dto.id=el.id;
		dto.title=el.title;
		dto.description=el.description;
		dto.dateCreated=el.dateCreated;
		
		return dto
	}
	
	TurDTO deleteElement(Long tur_id){
		def el=Tur.get(tur_id)
		el.delete()
	}
	
	public void addElement(Long tur_id,String tur_title,String tur_description)  throws ValidateException {	
		
		def company=Company.get(tur_id)
	
		println("Add Tur SERVER")		
		if (company) {	
			Tur newTur=new Tur()
			newTur.title=tur_title
			newTur.description=tur_description
			newTur.dateCreated=new Date()
			newTur.company = company
			
			if (!newTur.save()){									
				def err = newTur.errors.allErrors.collect{[
					errorMessage:messageSource.getMessage(it, null)							
				]}							
					throw new ValidateException(err)				
				}				
			}	
			
		}		
	
	
	public void editElement(Long tur_id,String tur_title,String tur_description, String tur_dateCreated) throws  ValidateException{
		println("Edit Tur SERVER")
		def tur=Tur.get(tur_id)
		tur.title=tur_title
		tur.description=tur_description
		if (tur_dateCreated){
			tur.dateCreated = new Date(tur_dateCreated)
		}else{
			tur.dateCreated = null
		}	
					
		if (!tur.save()){			
				def err = tur.errors.allErrors.collect{[
					errorMessage:messageSource.getMessage(it, null)			
				]}
				throw new ValidateException(err)				
			}			
		}	
	}
	





